﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using YJ.JobSchedule.Configuration;
using YJ.JobService.Jobs;

namespace YJ.JobService
{
    public partial class AddReflectionJob : Form
    {

        private MainForm mainForm;
        public AddReflectionJob(MainForm mainForm)
        {
            this.mainForm = mainForm;
            InitializeComponent();
        }

        private async void button1_Click(object sender, EventArgs e)
        {
            #region 验证
            if (JobConfiguationManager.IsExists(txtJobKey.Text))
            {
                MessageBox.Show("任务Key已经存在!");
                return;
            }

            if (txtJobKey.Text.IsNullOrWhiteSpace())
            {
                MessageBox.Show("任务Key不能为空!");
                return;
            }
            if (txtJobName.Text.IsNullOrWhiteSpace())
            {
                MessageBox.Show("任务名称不能为空!");
                return;
            }
            if (txtAssemblyPath.Text.IsNullOrWhiteSpace())
            {
                MessageBox.Show("程序集不能为空!");
                return;
            }
            if (txtMethod.Text.IsNullOrWhiteSpace())
            {
                MessageBox.Show("方法名称不能为空!");
                return;
            }
            if (txtNameSpace.Text.IsNullOrWhiteSpace())
            {
                MessageBox.Show("命名空间不能为空!");
                return;
            }
            string assemblyFullPath = Path.Combine(txtApplicationDir.Text, txtAssemblyPath.Text);
            if (!File.Exists(assemblyFullPath))
            {
                MessageBox.Show("程序集不存在,请检查!");
                return;
            }

            Assembly ass;
            try
            {
                ass = Assembly.LoadFrom(assemblyFullPath);
            }
            catch (Exception ex)
            {
                MessageBox.Show("程序集错误,请检查!");
                return;
            }
            if (ass == null)
            {
                MessageBox.Show("程序集错误,请检查!");
                return;
            }
            else
            {
                try
                {
                    var type = ass.GetType(txtNameSpace.Text, false);
                    if (type == null)
                    {
                        MessageBox.Show("类命名空间不存在,请检查!");
                        return;
                    }

                    var method = type.GetMethod(txtMethod.Text);
                    if (method == null)
                    {
                        MessageBox.Show("方法不存在,请检查!");
                        return;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("添加反射任务出现错误,详情如下:" + ex.Message);
                    return;
                }
            }

            #endregion

            ReflectionJob r = new ReflectionJob();
            r.AssemblyPath = txtAssemblyPath.Text;
            r.ClassNameSpace = txtNameSpace.Text;
            r.MethodName = txtMethod.Text;
            r.JobKey = txtJobKey.Text;
            r.ApplicationDirectory = txtApplicationDir.Text;
            r.JobName = txtJobName.Text;
            r.Description = txtDes.Text;


            await JobConfiguationManager.SaveJobConfiguration(new JobConfiguration
            {
                Group = null,
                IsDisabled = true,
                JobKey = r.JobKey,
                Name = txtJobName.Text,
                Description = txtDes.Text
            });

            await mainForm.AddReflectionJobs(r);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "DotNet程序集|*.dll;*.exe";
            openFileDialog1.Multiselect = false;
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                var fileName = openFileDialog1.FileName;
                txtApplicationDir.Text = Path.GetDirectoryName(fileName);
                txtAssemblyPath.Text = Path.GetFileName(fileName);
            }
        }
    }
}
